var WxParse = require('../../common/wxParse/wxParse.js');
let App = getApp()
Page({
  data: {
    order: {},
    menuTapCurrent: 0,
  },
  onLoad(option) { 
    console.log("option", option)
    
    this.setData({
      id: option.id,
      orderType: option.orderType
    })
    this.getOrderDetail(option.id, option.orderType)
  },

  getOrderDetail(id, orderType) {
    App.HttpService.prodOrderDetail({
      id: id,
      orderType: orderType
    }).then(res => { // orderType 0 普通订单 2 拼团
      let data = res.data
      if (data.code == 200) {
        this.setData({
          order: data.data
        })
        WxParse.wxParse('article', 'html', data.data.serviceFeedback, this, 5)
        this.countDown() // 执行倒计时函数
      }
    })
  },



  toPay() { // 付款
    let {
      id,
      orderType
    } = this.data
    let that = this
    App.HttpService.pay({
      orderId: id
    }).then(res => {
      let data = res.data
      if (data.code == 200) {
        let config = data.data
        let payconfig = {
          timeStamp: config.timeStamp,
          nonceStr: config.nonceStr,
          package: config.package,
          signType: config.signType,
          paySign: config.paySign,
        }
        payconfig.success = () => {
          that.getOrderDetail(id, orderType)
        }
        wx.requestPayment(payconfig)
      }
    })
  },
  toRefound() { // 退款

    let {
      id,
      orderType
    } = this.data
    let that = this
    App.HttpService.tuikuan({
      id: id
    }).then(res => {
      let data = res.data
      if (data.code == 200) {
        wx.showToast({
          title: '申请成功',
          icon: 'success',
          duration: 1000
        })
        that.getOrderDetail(id, orderType)
      }
    })

  },
  timeFormat(param) { // 小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() { // 倒计时函数
    let {
      order
    } = this.data
    let newTime = new Date().getTime() // 获取当前时间，同时得到活动结束时间数组
    let endTime = new Date(order.groupEndDate).getTime() // 对结束时间进行处理渲染到页面
    let obj = null
    // 如果活动未结束，对时间进行处理
    if (endTime - newTime > 0) {
      let time = (endTime - newTime) / 1000
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24))
      let hou = parseInt(time % (60 * 60 * 24) / 3600)
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60)
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60)
      obj = {
        day: this.timeFormat(day),
        hou: this.timeFormat(hou),
        min: this.timeFormat(min),
        sec: this.timeFormat(sec)
      }
    } else { // 活动已结束，全部设置为'00'
      obj = {
        day: '00',
        hou: '00',
        min: '00',
        sec: '00'
      }
    }
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      groupEndDate: obj
    })
    setTimeout(this.countDown, 1000)
  },
  // 选项卡
  menuTap: function(e) {
    var current = e.currentTarget.dataset.current; //获取到绑定的数据
    //改变menuTapCurrent的值为当前选中的menu所绑定的数据
    this.setData({
      menuTapCurrent: current
    });


  },
  dianhua: function() {
    var s = this.data.order.servicePhone

    wx.makePhoneCall({
      phoneNumber: s,
    })
  },

  // 拼团团购的付款
  fukuan:function(){



   
    App.HttpService.zzzf({

   orderId:this.data.id,
endtime:this.data.order.groupEndDate
     
    }).then(res => { 
     

      var n = res.data.code


      if (n == 200) {
        var config = res.data.data
        let payconfig = {
          timeStamp: config.timeStamp,
          nonceStr: config.nonceStr,
          package: config.package,
          signType: config.signType,
          paySign: config.paySign,
        }

        wx.requestPayment({
          timeStamp: config.timeStamp,
          nonceStr: config.nonceStr,
          package: config.package,
          signType: config.signType,
          paySign: config.paySign,
          success(res) {
            wx.showToast({
              title: '支付成功',
              duration: 1000,
            })
            
wx.navigateBack({
  delta: 2,  // 返回上一级页面。
  success: function() {
      console.log('成功！')
  }
})
            console.log(res, "成功")
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              icon: 'loading',
              duration: 1000,
            })
            console.log(res, "支付失败")

          }
        })



      } else {
        


        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return false;



      }
   
    


    })

    

  },



})